
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

package com.hlkj.pay.app.common.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.common.ITemplateAppService;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.dto.common.TemplateQueryDto;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.infrastructure.model.common.TemplateDO;
import com.hlkj.pay.service.common.IBusinessLogService;
import com.hlkj.pay.service.common.ITemplateService;
import com.hlkj.pay.vo.common.req.AddTemplateReq;
import com.hlkj.pay.vo.common.req.UpdateTemplateReq;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * Description: 模板 APP聚合业务实现
 *
 * @author monk
 * @date 2023/3/23
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TemplateAppServiceImpl implements ITemplateAppService {

    private final ITemplateService templateService;

    private final IBusinessLogService businessLogService;

    @Override
    public CommonResult<String> addTemplate(AddTemplateReq addTemplateReq) {
        log.info("addTemplate addTemplateReq:{}", JsonUtils.toJsonString(addTemplateReq));
        try {
            TemplateQueryDto templateQueryDto = new TemplateQueryDto();
            templateQueryDto.setTemplateType(addTemplateReq.getTemplateType());
            templateQueryDto.setTemplateName(addTemplateReq.getTemplateName());
            List<TemplateDO> templateList = templateService.queryList(templateQueryDto);
            if (!CollectionUtils.isEmpty(templateList)) {
                return CommonResult.error(CommonResultCode.TEMPLATE_NAME_ALREADY_EXISTS);
            }
            TemplateDO templateDO = BeanUtil.copyProperties(addTemplateReq, TemplateDO.class);
            String templateCode = templateService.addTemplate(templateDO);
            return CommonResult.success(templateCode);
        }
        catch (Exception e) {
            log.error("addTemplate error:{}", e.getMessage(), e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    @Override
    public CommonResult<Void> updateTemplate(UpdateTemplateReq addTemplateReq) {
        log.info("updateTemplate addTemplateReq:{}", JsonUtils.toJsonString(addTemplateReq));
        try {
            TemplateDO detail = templateService.detail(addTemplateReq.getTemplateCode());
            if (detail == null) {
                return CommonResult.error(CommonResultCode.TEMPLATE_DOES_NOT_EXIST);
            }

            TemplateQueryDto templateQueryDto = new TemplateQueryDto();
            templateQueryDto.setTemplateType(addTemplateReq.getTemplateType());
            templateQueryDto.setTemplateName(addTemplateReq.getTemplateName());
            List<TemplateDO> templateList = templateService.queryList(templateQueryDto);
            if (!CollectionUtils.isEmpty(templateList)) {
                TemplateDO templateDO = templateList.get(0);
                if (!templateDO.getTemplateCode().equals(detail.getTemplateCode())) {
                    return CommonResult.error(CommonResultCode.TEMPLATE_NAME_ALREADY_EXISTS);
                }
            }
            TemplateDO templateDO = BeanUtil.copyProperties(addTemplateReq, TemplateDO.class);
            templateDO.setTemplateCode(addTemplateReq.getTemplateCode());
            templateService.updateTemplate(templateDO);
            // 变更日志
            businessChangeLog(detail, templateDO);
            return CommonResult.success();
        }
        catch (Exception e) {
            log.error("updateTemplate error:{}", e.getMessage(), e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    @Override
    public CommonResult<Void> deleteTemplate(String templateCode) {
        log.info("deleteTemplate templateCode:{}", templateCode);
        try {
            if(!StringUtils.hasText(templateCode)){
                return CommonResult.error(CommonResultCode.PARAMETER_MISSING);
            }
            TemplateDO beforeValue = templateService.detail(templateCode);
            if (beforeValue == null) {
                return CommonResult.error(CommonResultCode.TEMPLATE_DOES_NOT_EXIST);
            }
            templateService.deleteTemplate(templateCode);
            // 变更日志
            TemplateDO afterValue = BeanUtil.copyProperties(beforeValue, TemplateDO.class);
            afterValue.setDeleteFlag(Boolean.TRUE);
            businessChangeLog(beforeValue, afterValue);
            return CommonResult.success();
        }
        catch (Exception e) {
            log.error("updateTemplate error:{}", e.getMessage(), e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    @Override
    public CommonResult<Void> statusUpdate(String templateCode, CommonEnum.TEMPLATE_STATUS templateStatus) {
        log.info("statusUpdate templateCode:{}", templateCode);
        try {
            TemplateDO beforeValue = templateService.detail(templateCode);
            if (beforeValue == null) {
                return CommonResult.error(CommonResultCode.TEMPLATE_DOES_NOT_EXIST);
            }
            switch (templateStatus) {
                case ABLE:
                    CommonEnum.TEMPLATE_TYPE templateType = CommonEnum.TEMPLATE_TYPE.from(beforeValue.getTemplateType());
                    switch (templateType) {
                        case EMAIL:
                        case AIEMAIL:
                        case WECHAT:
                            // 邮箱 微信 提交审核
                            break;
                        default:
                            break;
                    }
                    break;
                default:
            }
            templateService.statusUpdate(templateCode, templateStatus);
            // 变更日志
            TemplateDO afterValue = BeanUtil.copyProperties(beforeValue, TemplateDO.class);
            afterValue.setStatus(templateStatus.getCode());
            businessChangeLog(beforeValue, afterValue);
            return CommonResult.success();
        }
        catch (Exception e) {
            log.error("updateTemplate error:{}", e.getMessage(), e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    /**
     * log object
     * 
     * @param beforePayChannel
     * @param afterPayChannel
     * @return
     */
    void businessChangeLog(TemplateDO beforePayChannel, TemplateDO afterPayChannel) {
        businessLogService.addBusinessChangeLog(beforePayChannel.getTemplateCode(), CommonEnum.CHANGE_LOG_TYPE.MESSAGE_TEMPLATE.getCode(),
                CommonEnum.CHANGE_LOG_MODEL_TYPE.COMMON, beforePayChannel, afterPayChannel);
    }

}
